/**
 * 
 */
package com.android.demolayoutpages;

import java.util.List;

import com.android.demolayoutpages.R;
import com.android.demolayoutpages.R.drawable;
import com.android.demolayoutpages.R.id;
import com.android.demolayoutpages.R.layout;



import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.PorterDuff.Mode;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author INVEERA
 *
 */
public class MenuImageAdapter extends BaseAdapter {
	private static final String LOG_TAG = "MenuImageAdapter";
	 

	private Context context;
	private int width;
	private int height;



	public MenuImageAdapter(Context context) {
		this.context=context;

	}

	public int getCount() {
		return 12;
	}

	public Object getItem(int arg0) {


		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		View view;
		if (convertView==null) {
			//setUpLayout(position);

			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			Display display = wm.getDefaultDisplay();
			width  = display.getWidth();
			height = display.getHeight();
			int gridLayoutWidth  =  width*6/10;
			int gridLayoutHeight =  height*6/10;
			int gridWidth =  gridLayoutWidth/3;
			int gridHeight = gridLayoutHeight/3;
			int gridImageWidth  = gridWidth*9/10;
			int gridImageHeight = gridWidth*9/10;
			//int gridTextWidth  =  gridWidth*2/10;
			//int gridTextHeight =  gridHeight*2/10;

			/*Log.d(LOG_TAG, "gridHeight"+gridHeight);
			Log.d(LOG_TAG, "gridWidth"+gridWidth);
			Log.d(LOG_TAG, "gridImageHeight"+gridImageHeight);
			Log.d(LOG_TAG, "gridImageWidth"+gridImageWidth);
			Log.d(LOG_TAG, "gridTextHeight"+gridTextHeight);
			Log.d(LOG_TAG, "gridTextWidth"+gridTextWidth);
			 */


			view = inflater.inflate(R.layout.activity_main, null);

			/*ViewHolder.layout=(LinearLayout) view.findViewById( R.id.layout_gifts);
			ViewHolder.layout.setLayoutParams(new GridView.LayoutParams(100, 100));*/
			ImageView imgView = (ImageView)view.findViewById(R.id.imageView_rounded);
			TextView txtView = (TextView)view.findViewById(R.id.textView1);
			//txtView.setLayoutParams(new LinearLayout.LayoutParams(context., gridTextHeight));
			imgView.setLayoutParams(new LinearLayout.LayoutParams(gridImageWidth, gridImageHeight));
			Bitmap bm = getRoundedRectBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.gap_logo), 200);
			imgView.setImageBitmap(bm );
			imgView.setBackgroundResource(R.drawable.circle_shape);
			ViewHolder.layout=(LinearLayout) view.findViewById( R.id.layout_gifts);
			ViewHolder.layout.setLayoutParams(new GridView.LayoutParams(gridWidth, gridHeight));
			txtView.setText("25% off");
			/*ViewHolder.imageView=(ImageView) view.findViewById(R.id.imageView);
				ViewHolder.textView=(TextView) view.findViewById(R.id.textView);
				ViewHolder.imageView.setImageResource(gridImages[position]);
				ViewHolder.textView.setText(listCategories.get(position).getTitle());*/
		}else

			view=convertView;
		return view;
	}


	public Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
		Bitmap result = null;
		try {
			result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
					Bitmap.Config.ARGB_8888);
			Canvas canvas = new Canvas(result);

			
			int color = 0xff424242;
			Paint paint = new Paint();
			Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
			RectF rectF = new RectF(rect);
			int roundPx = pixels;

			paint.setAntiAlias(true);
			canvas.drawARGB(0, 0, 0, 0);
			paint.setColor(color);
			canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(bitmap, rect, rect, paint);
		} catch (NullPointerException e) {
			// return bitmap;
		} catch (OutOfMemoryError o) {
		}
		return result;
	}

	public static  class ViewHolder{
		static LinearLayout layout;
		static ImageView imageView;
		static TextView textView;
	}

}
